<template>
  <div>
    <kendoForm
      :kendo-form-items="kendoFormItems"
      :kendo-form="kendoForm"
      :kendo-params-value="kendoParamsValue"
    />
  </div>
</template>

<script>
  import kendoForm from '@/Views/components/kendo-vue-component/kendoForm/index.vue'
  export default {
    name: 'PrjOperateCmpt',
    components: {
      kendoForm
    },
    data() {
      return {
        dialogName: this.$attrs.data.dialogName,
        kendoFormItems: [
          {
            ref: 'PRJ_CD',
            remoteData: true,
            cascade: true,
            name: 'relPrjNo',
            label: '关联项目名称',
            paramName: 'relPrjNo',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            required: true,
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/prjMng/prjTransDict',
            autoBind: true,
            schema: {
              data: function(response) {
                if (typeof response.data === 'object') {
                  if (JSON.stringify(response.data) === '{}') {
                    return []
                  }
                }
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'PRJ_CD' })
              }
            }
          },
          {
            name: 'memberNo',
            label: '项目成员编号',
            paramName: 'memberNo',
            required: true,
            controlType: 'input'
          },
          {
            name: 'memberName',
            label: '项目成员名称',
            paramName: 'memberName',
            required: true,
            controlType: 'input'
          },
          {
            ref: 'MEMBER_TYPE_CD',
            remoteData: true,
            cascade: true,
            name: 'memberType',
            label: '成员类型',
            paramName: 'memberType',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            required: true,
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/sysMng/dict/findDictByTypeWithForm',
            autoBind: true,
            schema: {
              data: function(response) {
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'MEMBER_TYPE_CD' })
              }
            },
            change: this.wetherBowoutwith
          },
          {
            name: 'entranceDt',
            label: '入场日期',
            paramName: 'entranceDt',
            controlType: 'datePicker',
            format: 'yyyy-MM-dd',
            required: true
          },
          {
            ref: 'WETHER_BOWOUTWITH_CD',
            remoteData: true,
            cascade: true,
            name: 'wetherBowoutwith',
            label: '是否离场',
            paramName: 'wetherBowoutwith',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            required: true,
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/sysMng/dict/findDictByTypeWithForm',
            autoBind: true,
            schema: {
              data: function(response) {
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'WETHER_BOWOUTWITH_CD' })
              }
            },
            change: this.wetherBowoutwith
          },
          {
            ref: 'WETHER_GRADING_CD',
            remoteData: true,
            cascade: true,
            name: 'wetherGrading',
            label: '是否定级',
            paramName: 'wetherGrading',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            required: true,
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/sysMng/dict/findDictByTypeWithForm',
            autoBind: true,
            schema: {
              data: function(response) {
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'WETHER_GRADING_CD' })
              }
            }
          },
          {
            ref: 'MEMBER_GRADING_CD',
            remoteData: true,
            cascade: true,
            name: 'memberGrading',
            label: '人员定级',
            paramName: 'memberGrading',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            required: true,
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/sysMng/dict/findDictByTypeWithForm',
            autoBind: true,
            schema: {
              data: function(response) {
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'MEMBER_GRADING_CD' })
              }
            }
          },
          {
            label: '人员单价',
            name: 'unitPrice',
            paramName: 'unitPrice',
            controlType: 'numeric',
            min: 0,
            step: 1,
            format: '{0:c3}'
          }
        ],
        kendoForm: {
          ref: 'prodForm',
          valid: 'valid',
          buttons: [
            {
              name: '保存', // 按钮内容
              class: 'k-primary', // 按钮样式
              icon: 'save',
              click: this.save
            },
            {
              name: '重置', // 按钮内容
              icon: 'reset-sm',
              click: this.reset
            }
          ]
        },
        kendoParamsValue: {
          id: '',
          relPrjNo: '',
          wetherGrading: '',
          memberGrading: '',
          unitPrice: '',
          memberName: '',
          memberNo: '',
          memberType: '',
          entranceDt: '',
          wetherBowoutwith: '',
          bowoutwithDt: ''
        }
      }
    },
    mounted() {
      const operatorType = this.$attrs.data.operatorType
      if (operatorType === 'edit') {
        this.kendoParamsValue = Object.assign(this.kendoParamsValue, this.deepCopy(this.$attrs.data.param))
      }
    },
    methods: {
      save(paramVal, validator) {
        if (validator.validate()) {
          this.$post('/service/prjMng/prjMemberMng/savePrjMemberInfo', paramVal).then(res => {
            if (res.code !== 200) {
              this.$KendoNotify({
                title: '错误信息:' + res.code,
                message: res.message
              }, 'error')
            } else {
              this.$KendoNotify({
                title: '提示信息:',
                message: res.message
              }, 'success')
              this.$bus.emit('dialog' + `${this.dialogName}`, true)
            }
          })
        }
      },
      reset(paramVal) {
        Object.keys(paramVal).forEach(key => {
          if (Array.isArray(paramVal[key])) {
            this.kendoParamsValue[key] = []
          } else {
            this.kendoParamsValue[key] = ''
          }
        })
      },
      wetherBowoutwith(e) {
        const value = e.sender.value()
        if (value === '01') {
          const index = this.kendoFormItems.map(s => s.name).indexOf('bowoutwithDt')
          if (index > -1) {
            this.kendoFormItems.splice(index, 1)
          }
        }
        if (value === '02') {
          const index = this.kendoFormItems.map(s => s.name).indexOf('bowoutwithDt')
          if (index < 0) {
            const formItem = {
              name: 'bowoutwithDt',
              label: '离场日期',
              paramName: 'bowoutwithDt',
              controlType: 'datePicker',
              format: 'yyyy-MM-dd',
              required: true
            }
            this.kendoFormItems.push(formItem)
          }
        }
      }
    }
  }
</script>

<style>

</style>
